package com.eschool.beans;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.model.ForumPost;
import com.eschool.service.CustomerService;
import com.eschool.service.impl.CustomerServiceImpl;
import com.eschool.util.UserUtil;

@ManagedBean(name="forumBean")
@ViewScoped
public class ForumBean {
	
	private CustomerService service;
	private List<ForumPost> forumPosts;
	private String topic;
	private String message;

	public ForumBean() {
		topic = new String();
		service = new CustomerServiceImpl();
		forumPosts = service.getForumPosts(UserUtil.currentUser().getBranch().getBranchId());
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<ForumPost> getForumPosts() {
		return forumPosts;
	}

	public void setForumPosts(List<ForumPost> forumPosts) {
		this.forumPosts = forumPosts;
	}
	
	public String post() {
		ForumPost tempPost = new ForumPost();
		tempPost.setSubject(topic);
		tempPost.setParentPost(null);
		tempPost.setMessage(message);
		tempPost.setUser(UserUtil.currentUser());
		tempPost.setPostTime(new Timestamp(new Date().getTime()));
		
		service.addForumPost(tempPost);
		setTopic(""); // -- Setting the topic string to null after adding the topic to db
		
		String message = "Forum Post Successfully Added";
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,null, message));
		
		return "Forum";
	}

}
